// components/common/menu.js
import Dialog from '@vant/weapp/dialog/dialog';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    menu: Array,
    flag: {
      type: Boolean,
      value: false,//按钮是否显示
    },
    detailstatus: { //是否可以点击跳转到详情页
      type: Boolean,
      value: false,//按钮是否显示
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    idx: 0,//循环的下标
    id: "",//按钮的类型，上架，编辑，删除
    color: "#5EB95E",
    flag2: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 上传饭菜
    upload:function(e) {
      let that = this
      let upload = e.currentTarget.dataset.value
      let idx = e.currentTarget.dataset.idx//上传的id
      Dialog.confirm({
        context: this,
        title: '友情提示',
        message: '确认'+upload+'该饭菜么',
      }).then(() => {
        let meal_status='menu['+idx+'].meal_status';
        that.setData({
          [meal_status] : !that.data.menu[idx].meal_status
        })
        wx.cloud.callFunction({
          name: 'lu',
          data: {
            action: 'modifystatus',
            _id:that.data.menu[idx]._id,
            status: that.data.menu[idx].meal_status
          },
          success(res) {
            console.log("云函数修改数据成功",res)
          },
          fail(err) {
            console.log("函数修改数据失败",err)
          }
        })
      });
    },
    // 删除饭菜
    delete:function(e) {
      let that = this
      var idx = e.currentTarget.dataset.idx//上传的id
      Dialog.confirm({
        context: this,
        title: '友情提示',
        message: '确认删除该饭菜么',
      }).then(() => {
        wx.cloud.callFunction({
          name: 'lu',
          data: {
            action: 'delmeal',
            _id:that.data.menu[idx]._id
          },
          success(res) {
            console.log("云函数删除数据成功",res)
          },
          fail(err) {
            console.log("函数删除数据失败",err)
          }
        })
        let menu= that.data.menu;
        menu.splice(idx,1)
        this.setData({
          menu: menu,
        })
      });
    },

    goRefreshMenu: function() {
      console.log("饭菜管理了..")
      wx.navigateTo({
        url: '/pages/canteen/manage/manage',
      })
    },

    // 弹出框取消按钮
    cancel:function() {
      console.log("点击了取消按钮")
    },

    // 编辑饭菜
    edit: function(e) {
      let that = this
      var idx = e.currentTarget.dataset.idx//上传的id
      let _id = that.data.menu[idx]._id
      wx.navigateTo({
        url: '/pages/canteen/edit/edit?_id=' + _id,
      })
    },

    // 饭菜详情
    goToDetail:function(event) {
      if(this.data.detailstatus) {
        let meal_id = event.currentTarget.dataset.meal_id
        wx.navigateTo({
          url: '/pages/goods/index?id=' + meal_id,
        })
      }
    },
    
  },
  // 生命周期
  lifetimes: {
    ready: function() {
      let menus = this.data.menu
      let c = 0
      for(let menu of menus) {
        if(menu.meal_status == 1) {
          c++
        }
      }
      if(menus.length>0 && c === menus.length) {
        this.setData({
          flag2: true
        })
      }
    },
    attached: function() {
      console.log('在组件实例进入页面节点树时执行')
    },
    detached: function() {
      console.log('在组件实例被从页面节点树移除时执行')
    },
  },
})
